<template>
  <div class="creationType">
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      size="small"
    >
      <el-form-item>
        <el-button type="primary" @click="back">返回</el-button>
      </el-form-item>
      <el-form-item label="创作类型名称">
        <el-input
          v-model="formInline.creationTypeName"
          placeholder="创作类型名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      height="680px"
      border
      style="width: 100%"
      row-key="id"
      :tree-props="{
        children: 'creationTypeModels',
      }"
    >
      >
      <el-table-column prop="creationTypeName" label="创作类型名称">
      </el-table-column>
      <el-table-column prop="sort" label="创作类型顺序"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="add('add', scope.row)"
            v-if="parentId == scope.row.parentId"
            >创建子类型</el-button
          >
          <span
            v-else>
              
          <el-button
            type="text"
            @click="add('edit', scope.row)"
            >修改</el-button
          >
          <el-button
            type="text text-danger"
            @click="del(scope.row.id)"
            >删除</el-button
          >
          </span>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="block pageCont">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div> -->
    <el-dialog
      :title="type == 'add' ? '新增' : '编辑'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form
        :model="form"
        ref="form"
        label-width="100px"
        class="demo-ruleForm"
        size="small"
      >
        <el-form-item label="父级ID" style="display: none">
          <el-input v-model="form.parentId" placeholder="父级ID"></el-input>
        </el-form-item>
        <el-form-item label="创作类型名称">
          <el-input
            v-model="form.creationTypeName"
            placeholder="创作类型名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="创作类型顺序">
          <el-input-number
            v-model="form.sort"
            label="创作类型顺序"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm()"
        size="small">取 消</el-button>
        <el-button type="primary" @click="submit()"
        size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  CreationTypeListByTypeId,
  CreationTypeDel,
  CreationTypeEdit,
} from "@/api/creationType";
import _ from "lodash";
export default {
  data() {
    return {
      tableData: [],
      formInline: {},
      currentPage: 1,
      total: 0,
      pageSize: 20,
      parentId: 1,
      type: "add",
      form:{},
      dialogVisible:false
    };
  },
  created() {
    this.parentId = this.$route.query.typeId;
    this.getList();
  },
  methods: {
    back() {
      this.$router.back();
    },
    handleSizeChange() {},
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    getList() {
      CreationTypeListByTypeId({
        page: this.currentPage,
        ...this.formInline,
        typeId: this.parentId,
      }).then((res) => {
        _.forEach(res.data, (v, k) => {
          v["creationTypeName"] = v.creationTypeModel.creationTypeName;
          v["id"] = v.creationTypeModel.id;
          v["sort"] = v.creationTypeModel.sort;
          v["parentId"] = v.creationTypeModel.parentId;
        });
        this.tableData = res.data;
        this.total = res.data.total;
      });
    },
    del(id) {
      CreationTypeDel({ id: id }).then((res) => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.getList();
        } else {
          this.$message.error(res.data);
        }
      });
    },
    add(type, row) {
      this.type = type;
      this.dialogVisible = true;
      if (type=='edit'){
          this.form = JSON.parse(JSON.stringify(row));
      }else{
          this.form.parentId = row.id
          this.form.sort = row.creationTypeModels?row.creationTypeModels.length+1:1
      }
    },
    resetForm() {
      this.dialogVisible = false;
      this.form = {};
      this.$refs["form"].resetFields();
    },
    submit() {
      CreationTypeEdit(this.form).then((res) => {
        if (res.code == 0) {
          this.$message.success(this.type == "add" ? "新增成功" : "编辑成功");
          this.getList();
          this.resetForm();
        } else {
          this.$message.error(res.data);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.text-danger{
    color: #f56c6c;
}
</style>